# coding=utf-8

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import *
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from time import sleep
import re
import codecs
import sys
import os
import platform
import math
# import math
# import requests
# import json
from spider_user import *
from orphandrug_model import *
from spider_monitor import *
# from fateadm_api import FateadmApi
# from threading import Thread
from mysql import *


class Orphandrug:
    '''
    https://vip.yaozh.com/orphandrug
    药智网-孤儿药与罕见病
    @author spring
    @date 2022.01.21
    '''

    # 起始页
    __start_page = 'https://vip.yaozh.com/orphandrug'

    # 浏览器对象
    __browser = None

    # 当前页数
    __current_page = 1

    # 总页数
    __total_pages = 1

    # 最大访问页数
    __max_page = 30

    # 每页限制
    __limit = 49

    # 当前条数，用来处理点击到第几个了
    __current_index = 0

    # 进程名称
    __title = '药智网-孤儿药与罕见病'

    # 抓取状态
    __status = {'RUN': '抓取中', 'STOP': '停止', 'FINISH': '完成'}

    model = None

    monitor_model = None

    # 记录采集位置，格式：采集当前页数的第几条
    log_file = sys.path[0] + '/log_orphandrug.data'

    env_file = sys.path[0] + '/.env'

    def init_chrome(self):
        '''
        初始化浏览器
        '''

        # options
        chrome_options = webdriver.ChromeOptions()

        # chrome_options.add_argument('--headless')
        chrome_options.add_argument('--disable-gpu')
        chrome_options.add_argument('--no-sandbox')

        proxy = None

        if platform.system() == 'Windows':
            chrome_options.binary_location = "C:\Program Files\Google\Chrome\Application\chrome.exe"
        else:
            chrome_options.add_argument('--headless')
            chrome_options.add_argument('--disable-gpu')
            chrome_options.add_argument('--no-sandbox')
            # proxy = self.get_proxy_ip()

        if proxy:
            print('获取代理IP：', proxy)
            # 设置代理
            chrome_options.add_argument('--proxy-server=%s' % proxy)

        self.__browser = webdriver.Chrome(options=chrome_options)
        self.__browser.set_window_size(1100, 1050)

    def start(self):

        # 读取日志数据
        self.get_collect_log()

        self.monitor_model = SpiderMonitor()
        self.user = SpiderUser()

        # 获取当前脚本的用户信息
        self.userInfo = self.user.get('yaozh_orphandrug')

        # 浏览器这种不确定是不是不用代理就可以了，先不用代理尝试
        try:
            self.init_chrome()
            self.__browser.get(self.__start_page)
        except WebDriverException as err:
            print('初始化出错')
            os._exit(0)

        sleep(2)

        


        # 判断是否需要登录
        is_login = self.__browser.find_element_by_class_name('password-login')
        if is_login:
            # 关闭弹窗
            login_dialog = self.__browser.find_element_by_class_name('zjl_button')
            login_dialog.click()
            # 赋值输入框
            username = self.userInfo['username']
            pwd = self.userInfo['pwd']
            input_username = self.__browser.find_element_by_name('name')
            input_pwd = self.__browser.find_element_by_name('pwd')
            login_btn = self.__browser.find_element_by_class_name('loginButton')
            # 登录
            input_username.send_keys(username)
            input_pwd.send_keys(pwd)
            login_btn.click()
            # 异常提示点击弹窗强制登陆
            is_warn = self.__browser.find_element_by_class_name('el-dialog')
            if is_warn:
                print('have dialog')
                #elements = self.__browser.find_element_by_class_name('el-button--primary')
                # elements = self.__browser.find_elements(By.CLASS_NAME, 'el-button--primary')
                # for index in range(len(elements)):
                #     if index == 4:
                #         elements[index].click()
                # force_btn = self.__browser.find_element_by_xpath("//button[@class='el-button el-button--primary'][1]")
                # force_btn.click()
                #self.__browser.execute_script("document.getElementsByClassName('el-button el-button--primary')[1].firstChild.click()")
        sleep(5)
        # 跳转到孤儿药,从开头进入
        # self.__browser.execute_script(
        #     "document.getElementsByClassName('header-nav-item')[3].getElementsByClassName('submenu')[0].getElementsByTagName('ul')[0].getElementsByTagName('li')[14].getElementsByTagName('a')[0].click()")

        # 跳转到对应日期
        self.__browser.execute_script(
                "document.getElementsByClassName('logo')[0].href='/orphandrug?designateddate=20211213%3A20220211'")
        sleep(1)
        self.__browser.execute_script(
                "document.getElementsByClassName('logo')[0].target='_blank'")
        sleep(1)
        self.__browser.execute_script(
            "document.getElementsByClassName('logo')[0].click()")

        hands = self.__browser.window_handles  # 获取当前的所有句柄
        self.__browser.switch_to.window(hands[1])  # 转换窗口至最高的句柄
        sleep(5)

        # 切换每页条数为50条以减少请求次数,正式放开
        self.__browser.execute_script("document.getElementsByClassName('el-select__caret')[1].click();")

        self.__browser.execute_script(
            "document.getElementsByClassName('el-select-dropdown__list')[1].children[2].click()")

        sleep(3)
        # 选择认定日期，暂时不会，先处理数据，到30页

        # 取总页数
        self.totalNum = self.__browser.find_element_by_class_name('el-pagination__total').text

        self.totalCount = int(re.sub("\D", "", str(self.totalNum)))
        self.__total_pages = math.ceil(self.totalCount / 50)

        # 由于下面是通过最大页数来判断
        if self.__total_pages <= self.__max_page:
            self.__max_page = self.__total_pages
        print("total_pages：", self.__total_pages)

        # 点击下一页，如果是从库里拿出来的，那就给第二个元素赋值成对应页面，然后点击
        if int(self.__current_page) > 1:
            self.__browser.execute_script(
                "document.getElementsByClassName('number')[1].innerHTML='" + str(self.__current_page) + "'")
            sleep(1)
            self.__browser.execute_script(
                "document.getElementsByClassName('number')[1].click()")
            sleep(4)

        self.collect_list()

    def collect_list(self):

        self.monitor_model.save(self.__title, self.__status['RUN'])

        print('page：', self.__current_page, 'index:', self.__current_index)
        # 获取详情id
        self.code = ''
        detail_url = self.__browser.find_elements(By.CLASS_NAME, 'link-blue')[self.__current_index].get_attribute(
            'href')
        if detail_url:
            urls = detail_url.split("/");
            self.code = urls[4]
        # 数据处理过程
        self.__browser.execute_script(
            "document.getElementsByClassName('link-blue')[" + str(self.__current_index) + "].click()")

        hands = self.__browser.window_handles  # 获取当前的所有句柄
        #print(hands)  # 打印当前的所有句柄
        self.__browser.switch_to.window(hands[2])  # 转换窗口至最高的句柄
        sleep(4)

        tr_list = self.__browser.find_elements(By.TAG_NAME, 'tr')

        if isinstance(tr_list,list) == False:
            # 退出
            self.quit_process()
        # 基础字段变量定义
        data_from = ''
        common_name = ''
        active_component = ''
        orphan_name = ''
        yzorphan_name = ''
        confirm_date = ''
        confirm_statues = ''
        yzconfirm_statues = ''
        company_name = ''
        company_address = ''
        country = ''
        target = ''

        i = 0
        for tr in tr_list:
            tds = tr.find_elements(By.TAG_NAME, 'td')
            name = tds[0].text
            name = name.replace('(', '（')
            name = name.replace(')', '）')

            val = tds[1].text

            if name == '数据来源':
                data_from = val
            if name == '通用名称':
                common_name = val
            if name == '活性成分（中文）':
                active_component = val
            if name == '罕见病名':
                orphan_name = val
            if name == '罕见病名（药智）':
                yzorphan_name = val
            if name == '认定时间':
                confirm_date = val
            if name == '认定状态':
                confirm_statues = val
            if name == '认定状态（药智）':
                yzconfirm_statues = val
            if name == '企业名称':
                company_name = val
            if name == '企业地址':
                company_address = val
            if name == '隶属国家':
                country = val
            if name == '靶点':
                target = val

            i = i + 1

        # 入库
        self.model = Orphandrug_Model(
            id=self.code,
            data_from=data_from,
            common_name=common_name,
            active_component=active_component,
            orphan_name=orphan_name,
            yzorphan_name=yzorphan_name,
            confirm_date=confirm_date,
            confirm_statues=confirm_statues,
            yzconfirm_statues=yzconfirm_statues,
            company_name=company_name,
            company_address=company_address,
            country=country,
            target=target,
        )

        self.model.save()

        # 数据处理过程结束

        # 关闭当前页面，切换回初始页面
        self.__browser.close()
        self.__browser.switch_to.window(hands[1])

        if int(self.__current_index) < int(self.__limit):
            self.__current_index = int(self.__current_index) + 1
            # 保存日志
            self.save_collect_log()

            # 继续采集下一页
            self.collect_list()
        else:
            if int(self.__current_page) < int(self.__max_page):
                # 下一页
                self.__browser.find_element(By.CSS_SELECTOR, '.btn-next').click()
                self.__current_page = int(self.__current_page) + 1
                self.__current_index = 0
                sleep(3)

                # 保存日志
                self.save_collect_log()

                # 继续采集下一页
                self.collect_list()
            else:
                print('没有下一页，退出')

                self.__current_page = int(self.__current_page) + 1
                # 保存日志
                self.save_collect_log()

                # 退出
                self.quit_process()




    def save_collect_log(self):
        '''
        保存抓取位置
        '''
        with codecs.open(self.log_file, 'w', encoding='utf-8') as file:
            file_content = str(self.__current_page) + '-' + str(self.__current_index)
            file.write(file_content)
            file.close()

    def get_collect_log(self):
        '''
        获取抓取位置
        '''
        self.__current_page = 1

        if os.path.exists(self.log_file):
            with codecs.open(self.log_file, 'r', encoding='utf-8') as file:
                file_content = file.read(100)
                file.close()
                if len(file_content) > 0:
                    contents = file_content.split("-")
                    self.__current_page = int(contents[0])
                    self.__current_index = int(contents[1])
                    print('读取文件日志完成: 当前页：%s，当前条数：%s' % (self.__current_page , self.__current_index))
                else:
                    raise '日志格式不正确'
        else:
            print('日志文件不存在...')

    def quit_process(self):
        self.monitor_model.save(self.__title, self.__status['STOP'])
        self.__browser.quit()
        sys.exit()


starter = Orphandrug()
starter.start()
